import { Draw } from 'ol/interaction'
export default class DrawTool {
  constructor(map, vector) {
    this.map = map
    this.vector = vector
    this.draw = null
    this.drawCache = []
  }

  //绘制图形
  drawFun(typeName, type, callback) {
    if (this.draw) {
      this.map.removeInteraction(this.draw)
      this.map.removeInteraction(this.snap)
    }
    this.draw = new Draw({
      source: this.vector.getSource(),
      type: typeName
    })
    this.map.addInteraction(this.draw) //图形绘画
    this.draw.on(
      'drawend',
      (evt) => {
        this.drawCache.push(evt.feature)
        console.log(evt.feature)
        this.map.removeInteraction(this.draw)
        if (type) {
          callback(evt)
        }
      },
      this
    )
  }
  //结束绘制
  removeStop() {
    this.map.removeInteraction(this.draw)
  }
  //清除绘制
  clear() {
    this.removeStop()
    for (let i in this.drawCache) {
      this.vector.getSource().removeFeature(this.drawCache[i])
    }
  }
}
